const {
  toUrl,
  globalData
} = getApp();
import {
  request
} from "@/utils/request";
Page({
  toUrl,
  show: false,

  data: {
    isExit: false,

    globalData,
    width30: 10,
    orderList: [1, 2, 3],
    year: '',
    month: '',
    day: '',
    activeIndex: '',
    days: [{
      // IsThatDay:1,
      // IsAppointment:1,
      Reservation: 0,
      day: '01'
    }],
    minYear: '',
    minMonth: '',
    userInfo: {},
    imgbaseUrl: "",
    signIntegral: "",
    headBc: false,
    isShowDatePicker: true,
    pages: 1,
    integralDetail: [],
    count: 0,
    weekList: [{
      name: '日'
    }, {
      name: '一'
    }, {
      name: '二'
    }, {
      name: '三'
    }, {
      name: '四'
    }, {
      name: '五'
    }, {
      name: '六'
    }, ]
    // integral: {} //积分
  },



  integrallist() {
    if (this.data.pages > 1 && this.data.pages > this.data.count) {
      this.setData({
        isExit: true,
      });
      return;
    }
    let data = {
      type: 1,
      typeid: 2,
      page: this.data.pages,
    }
    request("integral/integrallist", {
      data
    }).then((res) => {
      let pages = this.data.pages + 1;
      if (res.errcode == 1) {
        if (this.data.pages > 1) {
          let listData = this.data.integralDetail.concat(res.data)
          this.setData({
            integralDetail: listData,
            pages,
            count: res.count
          })


        } else {
          this.setData({
            integralDetail: res.data,
            pages,
            count: res.count
          })
        }


      }
    });
  },

  onPageScroll(e) {
    const scrollTop = e.scrollTop
    if (scrollTop > 30) {
      this.setData({
        headBc: true,
      })
    } else {
      this.setData({
        headBc: false,
      })
    }
  },
  // //隐藏日期选择
  // hideDatePicker() {
  // 	this.setData({
  // 		isShowDatePicker: false,
  // 	});
  // },
  // handleSigin: function () {
  //   console.log('立即签到');
  // },
  handleSigin() {
    // if (this.data.signininfo.info.is_sign == 1) {
    //   wx.showToast({
    //     title: '已经签到过了',
    //     icon: 'none'
    //   })
    //   return
    // }
    request("integral/signin").then((res) => {
      if (res.errcode == 1) {
        this.setData({
          pages: 1
        });
        this.getDays()
        this.integrallist()

      }
    });
  },
  show() {
    this.setData({
      show: !this.data.show
    });
  },

  //上一月
  prevMonth() {
    if (this.data.month == this.data.minMonth && this.data.year == this.data.minYear) {
      return;
    }
    if (this.data.month == 1) {
      this.setData({
        year: this.data.year - 1,
        month: 12,
      });
    } else {
      this.setData({
        month: this.data.month - 1,
      });
    }
    this.getDays();
  },

  //下一月
  nextMonth() {
    if (this.data.month == 12) {
      this.setData({
        year: this.data.year + 1,
        month: 1,
      });
    } else {
      this.setData({
        month: this.data.month + 1,
      });
    }
    this.getDays();
  },

  //触发月份回调
  getHandleMonth() {
    let that = this
    this.getMonthDays(that.data.year, that.data.month).then((res) => {
      that.getHandleList(that.data.year, that.data.month, res)
    })
  },


  //根据年月获取日
  getDays() {
    let that = this;
    request('integral/signininfo')
      .then((res) => {
        // console.log(res);
        if (res.errcode === 1) {
          that.setData({
            signininfo: res.data
          });
          this.getHandleMonth()
        }
      });
  },
  //根据两个月份同一天的差值获取,同样是传入需要获取的对应年份和月份
  getMonthDays(year, month) {
    return new Promise((resolve, reject) => {
      let stratDate = new Date(year, month - 1, 1),
        endData = new Date(year, month, 1);
      let days = (endData - stratDate) / (1000 * 60 * 60 * 24);
      // return days;
      resolve(days)
    })
  },
  getHandleList(year, month, data) {
    // console.log(year, month, data);
    let arr = new Array(data)
    let arr1 = new Array(35)
    let carliserList = []
    for (let index = 0; index < arr.length; index++) {
      let weeknum = new Date(year + '-' +  `${month <= 9 ?'0':''}` + month + '-' + `${index < 9 ?'0':''}` + `${index + 1}`).getDay()
      // console.log(weeknum);
      const element = {
        id: index + 1,
        time: year + '-' + `${month < 9 ?'0':''}` + month + '-' + `${index < 9 ?'0':''}` + `${index + 1}`,
        day: `${index + 1}`,
        isgray: false,
        weeknum
      };

      carliserList.push(element)
    }
    // console.log(carliserList,'################');
    if (carliserList[0].weeknum > 0) {
      let chanum = carliserList[0].weeknum
      let aa = this.getMonthDays(this.data.year, this.data.month - 1).then((res) => {
        for (let index = res; index > res - chanum; index--) {
          let element = {
            id: index,
            time: year + '-'  +  month + '-' + `${index < 9 ?'0':''}` + `${index}`,
            day: `${index}`,
            isgray: true,
            
          };
          carliserList.unshift(element)
        }
      })
      aa.then((res) => {
        this.setData({
          days: carliserList
        })
      })
    
    }
    // console.log(carliserList,'******');


    // console.log(carliserList[data - 1]);
    if (carliserList[data - 1].weeknum != 6) {

      let sumlen = 6
      let chanum = sumlen - carliserList[data - 1].weeknum
      // console.log(chanum);
      for (let index = 0; index < chanum; index++) {
        let element = {
          id: index + 1,
          time: year + '-' +  month + '-' +  `${index + 1}`,
          day: `${index + 1}`,
          isgray: true,
        };
        carliserList.push(element)

      }
    }
    carliserList.map((it,idx) => {
      this.data.signininfo.list.forEach((it1) => {
        if (it.time === it1.change_time) {
          it.Reservation = 1
        }
      })
    })

    this.setData({
      days: carliserList
    })
  },
  onLoad(options) {
    let that = this;
    let date = new Date();
    that.setData({
      year: date.getFullYear(),
      month: date.getMonth() + 1,
      minYear: date.getFullYear(),
      minMonth: date.getMonth() + 1,
    });
    this.getDays()
    this.integrallist()


    // console.log(this.getMonthDays(this.data.year, 2, ))
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    this.setData({
      userInfo: getApp().globalData.baseInfo,
      imgbaseUrl:getApp().globalData.baseUrl + "/static/images/siginBg.png"
      // baseUrl: getApp().globalData.baseUrl + "/static/images/me11.png",
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {


  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let isExit = this.data.isExit
    if (isExit) {
      return;
    } else {
      this.integrallist();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});